<?php
class CF_Acl_Role
{
	/**
	 * 
	 * @var string
	 */
	protected $_id;
	
	/**
	 * @var CF_Acl
	 */
	protected $_acl;
	
	/**
	 * @var array
	 */
	protected $_parents = array();
	
	/**
	 * 
	 * @param string $id
	 * @param CF_Acl $acl
	 * @return void
	 */
	public function __construct($id, CF_Acl $acl, $parents = array())
	{
		$this->_id = (string) $id;
		$this->addParent($parents);
	}
	
	/**
	 * @return string
	 */
	public function getId()
	{
		return $this->_id;
	}
	
	/**
	 * 
	 * @param string|CF_Acl_Role $role
	 * @return boolean
	 */
	public function hasParent($role)
	{
		return isset($this->_parents[(string) $role]);
	}
	
	/**
	 * 
	 * @param string $id
	 * @return CF_Acl_Role
	 */
	public function getParent($id)
	{
		return $this->_parents[$id];	
	}
	
	/**
	 * @return array
	 */
	public function getAllParents()
	{
		return $this->_parents;
	}
	
	/**
	 * 
	 * @param CF_Acl_Role $role
	 * @return CF_Acl_Role $this
	 */
	public function addParent($roles)
	{
		if(is_array($roles)){
			foreach ($roles as $role){
				$this->addParent($role);
			}
			return $this;
		}
		
		$roleId = (string) $roles;
		if(!($roles instanceof CF_Acl_Role)){
			
			throw new CF_Acl_Exception("Role \"$roleId\" must be a instance of CF_Acl_Role");
		}
		$this->_parents[$roleId] = $roles;
		return $this;
	}
	
	/**
	 * 
	 * @param string|CF_Acl_Role $role
	 * @return CF_Acl_Role $this
	 */
	public function removeParent($role)
	{
		unset($this->_parents[(string) $role]);
		return $this;
	}
	
	/**
	 * @return CF_Acl_Role $this
	 */
	public function removeAllParents()
	{
		$this->_parents = array();
		return $this;
	}
	
	/**
	 * @return string
	 */
	public function __toString()
	{
		return $this->getId();
	}
}